#define _CRT_SECURE_NO_WARNINGS
#include "SList.h";


SListNode* SListCreate(SListDataType val)
{
	SListNode* p = NULL;
	p = (SListNode*)malloc(sizeof(SListNode));
	if (p == NULL)
	{
		printf("创建失败.\n");
		return NULL;
	}
	p->data = val;
	p->next = NULL;
	return p;
}

SListNode* SListRemove(SListNode* node)
{
	if (node->next != NULL)
	{
		SListRemove(node->next);
	}
	free(node);
	return NULL;
}

SListNode* SListPushHead(SListNode* node, SListDataType val)
{
	if (node == NULL)
	{
		return SListCreate(val);
	}

	SListNode* p = (SListNode*)malloc(sizeof(SListNode));
	if (p == NULL)
	{
		printf("添加失败.\n");
		return NULL;
	}
	p->next = node;//指向下一个节点
	p->data = val;
	return p;
}

SListNode* SListPushBack(SListNode* node, SListDataType val)
{
	if (node == NULL)
	{
		return SListCreate(val);
	}
	SListNode* p = NULL;
	p = node;
	for (; p->next != NULL;)
	{
		p = p->next;
	}
	p->next = (SListNode*)malloc(sizeof(SListNode));
	if (p->next == NULL)
	{
		printf("添加失败.\n");
		return NULL;
	}
	p->next->next = NULL;//最末尾节点指向 NULL
	p->next->data = val;
	return node;
}

SListNode* SListDeleteHead(SListNode* node)
{
	if (node == NULL)
	{
		printf("链表为空.\n");
		return node;
	}
	SListNode* pret = node->next;
	free(node);

	return pret;
}

SListNode* SListDeleteBack(SListNode* node)
{
	if (node == NULL)
	{
		printf("链表为空.\n");
		return node;
	}
	SListNode* p = NULL;
	p = node;
	for (; p->next != NULL && p->next->next != NULL;)
	{
		p = p->next;
	}
	if (p->next != NULL)
	{
		free(p->next);
		p->next = NULL;//倒数第二个节点的 next 置空
		return node;
	}
	else
	{
		free(p);
		return NULL;
	}
}

int SListSearch(SListNode* node, SListDataType key)
{
	if (node == NULL)
	{
		printf("链表为空.\n");
		return -1;
	}
	int i = 0;
	SListNode* p = NULL;
	p = node;
	do
	{
		if (p->data == key)
		{
			return i;
		}
		i++, p = p->next;
	} while (p != NULL && p->next != NULL);

	return -1;
}

SListNode* SListInsert(SListNode* node, SListDataType val, int pos)
{
	if (node == NULL)
	{
		printf("链表为空.\n");
		return node;
	}
	else if (pos == -1)
	{
		return SListPushHead(node, val);
	}
	else if (pos < 0)
	{
		printf("插入失败:位置非法.\n");
		return node;
	}

	SListNode* p = NULL;
	p = node;
	for (; pos; pos--)
	{
		p = p->next;
		if (p == NULL)
		{
			printf("插入失败:位置非法.\n");
			return node;
		}
	}
	SListNode* pnew = (SListNode*)malloc(sizeof(SListNode));
	if (pnew == NULL)
	{
		printf("插入失败.\n");
		return node;
	}
	pnew->next = p->next;
	pnew->data = val;
	p->next = pnew;

	return node;
}

SListNode* SListDelete(SListNode* node, int pos)
{

	if (pos < 0)
	{
		printf("删除失败:位置非法.\n");
		return node;
	}
	else if (pos == 0)
	{
		return SListDeleteHead(node);
	}
	SListNode* p = NULL;
	SListNode* p2 = NULL;
	p = node;
	for (; ; pos--)
	{
		if (pos == 0)
		{
			if (p->next != NULL)
			{
				p2->next = p->next;
			}
			else
			{
				p2->next = NULL;
			}
			free(p);
			return node;
		}
		p2 = p;
		p = p->next;
		if (p == NULL)
		{
			printf("删除失败:位置非法.\n");
			return node;
		}
	}
}

void SListPrint(SListNode* node)
{
	SListNode* p = node;
	for (; p != NULL;)
	{
		printf("%d -> ", p->data);
		p = p->next;
	}
	printf("NULL\n");
}

// 1 2 3 4 5 6 7 null
//  p p
//  2








